package com.suny.lock;

/**
 * @author sunny
 * @date 2019/4/24
 */
public class LockTest {

    public static void main(String[] args) {
        LockClass lockClass = new LockClass();

        Thread a = new Thread(lockClass, "Thread-A");
        Thread b = new Thread(lockClass, "Thread-B");

        a.start();
        b.start();
    }

    private static class LockClass implements Runnable{
        private InnerLockClass innerLockClass = new InnerLockClass();

         private class InnerLockClass {
            private int x = 0;

            public synchronized void add() {
                for (int i = 1; i < 5; i++) {
                    x += i;
                    System.out.println(Thread.currentThread().getName() + ": ----- X= " + x);

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

            public synchronized void print() {
                System.out.println(Thread.currentThread().getName() + ": ===== X = " + x);
            }
        }

        @Override
        public void run() {
            innerLockClass.add();
            innerLockClass.print();

        }
    }
}
